package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.File;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import controller.PartitionChangerListener;
import controller.SelectBrowsePanelListener;


import model.GlobalParams;

public class BrowsePanel extends JPanel{
	
	private static final long serialVersionUID = 1L;
	
	public BrowsePanel(MainFrame f , String n) {
		name = n;
		frame = f;
		
		setComponents();
	}
	
	public void setComponents() {
		
		setLayout(new BorderLayout());	
		
		volumeChanger = new JPanel();
		relativePath = new JLabel();
		statusBar = new StatusBar(new File(System.getProperty("user.home")));
		
		if (GlobalParams.getInstance().defaultLook == 1)
			diskContent = new DiskContentPanel(frame,this,GlobalParams.getInstance().defaultLook);		
		else 
			tabPanel = new TabPannel(frame,this);
		
		configureRelativePath();
		configureVolumeChanger();
		
		southPanel = new JPanel();		
		
		add(volumeChanger,BorderLayout.NORTH);
		add(southPanel,BorderLayout.CENTER);
		
		southPanel.setLayout(new BorderLayout());
		southPanel.add(relativePath,BorderLayout.NORTH);
		southPanel.add(statusBar, BorderLayout.SOUTH);
		
		if (GlobalParams.getInstance().defaultLook == 1)
			southPanel.add(diskContent,BorderLayout.CENTER);
		else
			southPanel.add(tabPanel,BorderLayout.CENTER);
		
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void configureVolumeChanger() {
		
		volumeChanger.setLayout(new GridBagLayout());
		GridBagConstraints cc = new GridBagConstraints();		
		cc.fill = GridBagConstraints.HORIZONTAL ;  
		cc.gridx = 0;
        cc.gridy = 0;
        cc.weightx = 0.25; 
        
        File[] voulme = File.listRoots();  
        partition = new JComboBox(voulme);
        
        File home = new File(System.getProperty("user.home"));
        home.getAbsolutePath();
        
        File parent = home.getParentFile()  ;
        File tmp  = home.getParentFile();
        while (tmp != null) {
        	tmp = parent.getParentFile();    
        	if (tmp != null)
        		parent = tmp;
        }
        
        partition.setSelectedItem(parent);        
        volumeChanger.add(partition,cc); 
        volumeChanger.add(new JSeparator(SwingConstants.VERTICAL));  	
       
		cc.fill = GridBagConstraints.HORIZONTAL ;  
		cc.gridx = 1;
        cc.gridy = 0;
        cc.weightx = 0.75; 
        freeSpaceInformation = new JLabel("");
        freeSpaceInformation.setBackground(Color.blue);
        
        volumeChanger.add(freeSpaceInformation,cc); 
        volumeChanger.add(new JSeparator(SwingConstants.VERTICAL));		
        
        freeSpaceInformation.addMouseListener(new SelectBrowsePanelListener(getDiskContent(), frame,this));
        partition.addActionListener(new PartitionChangerListener(frame));	
		partition.addMouseListener(new SelectBrowsePanelListener(getDiskContent(), frame, this));
		volumeChanger.addMouseListener(new SelectBrowsePanelListener(getDiskContent(), frame, this));
	}
	
	public void setNonActive() {		
		getDiskContent().setNonActive();			
		relativePath.setBackground(Color.GRAY);
		relativePath.setForeground(Color.WHITE);
	}
	
	public void setActive() {		
		getDiskContent().setActive();		
		relativePath.setBackground(new Color(10,36,106));
		relativePath.setForeground(Color.WHITE);
	}	
	
	public void configureRelativePath() {	
		relativePath.setOpaque(true);
		relativePath.setText(System.getProperty("user.home"));
        relativePath.addMouseListener(new SelectBrowsePanelListener(getDiskContent(), frame,this));        	
	}
	
	public JLabel getRelativePath() {
		return relativePath;
	}

	public void setRelativePath(JLabel relativePath) {
		this.relativePath = relativePath;
	}
	
	public StatusBar getStatusBar() {
		return statusBar;
	}

	public void setStatusBar(File file) {
		this.statusBar.setInfoDir(file);
	}

	public String getName() {		
		return name;		
	}
	
	public int GetConntent() {
		return lookLike;
	}
	
	public void setDiskContent(int look) {	
		lookLike = look;
		removeAll();		
		setComponents();
		revalidate();		
	}
	
	public DiskContentPanel getDiskContent() {
		if (GlobalParams.getInstance().defaultLook == 1) {
			return diskContent;
		}else {			
			tabPanel.updateNames();
			return tabPanel.getPrimaryTab();
		}
		
	}		
	
	public String getActualPartition() {		
        return (String)partition.getSelectedItem();			
	}
	
	public void setActualPartitonToCommandLine() {		
		File f = (File)partition.getSelectedItem();
		frame.getCommandLineBar().setVolumeName(f.getAbsolutePath());		
	}
	
	public JLabel getFreeSpaceInformation() {
		return freeSpaceInformation;
	}

	public void setFreeSpaceInformation(String freeSpaceInformation) {
		this.freeSpaceInformation.setText(freeSpaceInformation);
	}
	
	public void updateNames(){
		getDiskContent().updateNames();
	}
	
	
	private JPanel southPanel;
	private JPanel volumeChanger;	
	private String name;
	private MainFrame frame;	
	@SuppressWarnings("rawtypes")
	private JComboBox partition;	
	private DiskContentPanel diskContent;
	private StatusBar statusBar;	
	private JLabel freeSpaceInformation;	
	private JLabel relativePath;	
	private int lookLike;
	private TabPannel tabPanel;
	

}
